#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2006                 ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   route4-redirect
#
# Description:
#   Verify the kernel is not crashed when the IPv4 route is modified by
#   ICMP Redirects frequently
#
# Setup:
#   See testcases/network/stress/README
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# History:
#	Apr 07 2006 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
# Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# The test case ID, the test case count and the total number of test case
TCID=route4-redirect01
TST_TOTAL=1
TST_COUNT=1
export TCID
export TST_COUNT
export TST_TOTAL

# Test description
tst_resm TINFO "Verify the kernel is not crashed when the IPv4 route is modified by  ICMP Redirects frequently"

# Make sure the value of LTPROOT
LTPROOT=${LTPROOT:-`(cd ../../../.. ; pwd)`}
export LTPROOT

# Check the environmanet variable
. check_envval || exit $TST_TOTAL

# The number of times where route is changed
NS_TIMES=${NS_TIMES:-10000}

# The number of the test link where tests run
LINK_NUM=${LINK_NUM:-0}

# Network portion of the IPv4 address
IPV4_NETWORK=${IPV4_NETWORK:-"10.0.0"}

# Netmask of for the tested network
IPV4_NETMASK_NUM=24

# Broadcast address of the tested network
IPV4_BROADCAST=${IPV4_NETWORK}.255

# Host portion of the IPv4 address
LHOST_IPV4_HOST=${LHOST_IPV4_HOST:-"1"}	# src
RHOST_IPV4_HOST="2"	# gateway

# The destination network
DST_NETWORK="10.10.0"   # destination network would be 10.10.0.0/24
DST_HOST="5"
DST_PORT="7"


#-----------------------------------------------------------------------
#
# NAME:
#   do_cleanup
#
# DESCRIPTION:
#   Recover the tested interfaces
#
#-----------------------------------------------------------------------
do_cleanup()
{
    # Kill the redirector utility
    $LTP_RSH $RHOST killall -SIGHUP ns-icmp_redirector >/dev/null 2>&1

    # Initialize the interfaces
    initialize_if lhost ${LINK_NUM}
    initialize_if rhost ${LINK_NUM}
}


#-----------------------------------------------------------------------
#
# NAME:
#   do_setup
#
# DESCRIPTION:
#   Set the initial route and start icmp redirect on the remote host
#
# SET VALUES:
#   rhost_ipv4addr      - IPv4 Address of the remote host
#   lhost_ifname        - Interface name of the local host
#   rhost_ifname        - Interface name of the remote host
#
#-----------------------------------------------------------------------
do_setup()
{
    # Make sure to clean up
    do_cleanup

    # Get the Interface name of local host
    lhost_ifname=`get_ifname lhost ${LINK_NUM}`
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to get the interface name at the local host"
	exit $TST_TOTAL
    fi

    # Get the Interface name of remote host
    rhost_ifname=`get_ifname rhost ${LINK_NUM}`
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to get the interface name at the remote host"
	exit $TST_TOTAL
    fi

    # Remove the link-local address of the remote host
    sleep 3
    $LTP_RSH $RHOST "ip addr flush dev $rhost_ifname" > /dev/null

    # Assign IPv4 address to the interface of the local host
    set_ipv4addr lhost ${LINK_NUM} ${IPV4_NETWORK} ${LHOST_IPV4_HOST}
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to assign an IPv4 address at the local host"
	return 1
    fi

    # Add route to the initial gateway
    route add -net ${DST_NETWORK}.0 netmask 255.255.255.0 gw ${IPV4_NETWORK}.${RHOST_IPV4_HOST} dev $lhost_ifname

    # Make sure the sysctl value is set for accepting the redirect
    sysctl -w net.ipv4.conf.${lhost_ifname}.accept_redirects=1 >/dev/null
    sysctl -w net.ipv4.conf.${lhost_ifname}.secure_redirects=0 >/dev/null

    # Run the redirector utility at the remote host
    ret=`$LTP_RSH $RHOST "${LTPROOT}/testcases/bin/ns-icmp_redirector -I $rhost_ifname -b ; "'echo $?'`
    if [ $ret -ne 0 ]; then
	tst_resm TBROK "Failed to run icmp redirector at the remote host"
	exit $TST_TOTAL
    fi
}



#-----------------------------------------------------------------------
#
# FUNCTION:
#   test_body
#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   None
#
#-----------------------------------------------------------------------
test_body()
{
    # Loop for changing the route
    cnt=0
    while [ $cnt -lt $NS_TIMES ]; do
	ns-udpsender -f 4 -D ${DST_NETWORK}.${DST_HOST} -p $DST_PORT -o -s 8
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to run udp packet sender"
	    return 1
	fi
	cnt=`expr $cnt + 1`
    done

    tst_resm TPASS "Test is finished correctly."
    return 0
}


#-----------------------------------------------------------------------
#
# Main
#
# Exit Value:
#   The number of the failure
#
#-----------------------------------------------------------------------
RC=0
do_setup
test_body || RC=`expr $RC + 1`
do_cleanup

exit $RC
